from django.db import models

# Create your models here.

NOTE_STATUS = (
 (-1, 'Unspecified'),
 (1, 'In Process'),
 (2, 'For Checking'),
 (3, 'Release to customer'),
 (4, 'To be archived'),
)

class Note(models.Model):
    """
    A note represents a note SAP is releasing for a issue

    >>> n_1372118 = Note.objects.create(noteId = 1372118, noteSubject = "Overview Note for SAP Business One 8.8 SP00 PL01")
    >>> n_1344907 = Note.objects.create(noteId = 1344907, noteSubject = "Overview Note for SAP Business One 8.8 SP00 PL02")
    >>> n_1378737 = Note.objects.create(noteId = 1378737, noteSubject = "Overview Note for SAP Business One 8.8 SP00 PL03")
    >>> n_1388288 = Note.objects.create(noteId = 1388288, noteSubject = "DB Structure Changes in SAP Business One 8.8 SP00 PL03")
    >>> n_1356876 = Note.objects.create(noteId = 1356876, noteSubject = "Collective note for 8.8 DB metadata changes")
    >>> n_1382740 = Note.objects.create(noteId = 1382740, noteSubject = "Tax Amount wrong on Credit Note for INV with DPM Invoice")

    # Check note id
    >>> Note.objects.get(noteId = 1378737).noteId
    1378737
    """
    noteId = models.IntegerField(primary_key = True)
    noteSubject = models.CharField(max_length=100)
    noteStatus = models.IntegerField(choices = NOTE_STATUS, default = -1)

class NoteLink(models.Model):
    """
    This handle notes which link to a patch overview note
    >>> n1378737 = Note.objects.get(noteId = 1378737)
    >>> n1388288 = Note.objects.get(noteId = 1388288)
    >>> n1382740 = Note.objects.get(noteId = 1382740)

    # Link
    >>> nl_1 = NoteLink.objects.create(overviewNote = n1378737, linkedNote = n1388288)
    >>> no_2 = NoteLink.objects.create(overviewNote = n1378737, linkedNote = n1382740)

    # query PL03 linked notes
    >>> len(NoteLink.objects.all())
    2
    >>> len(NoteLink.objects.filter(overviewNote = Note(noteId = 1378737)))
    2
    """
    id = models.AutoField(primary_key = True)
    overviewNote = models.ForeignKey(to = "Note", to_field="noteId", related_name = "overviewNote")
    linkedNote = models.ForeignKey(to = "Note", to_field="noteId", related_name = 'linkedNote')

class PatchInfo(models.Model):
    """
    PatchInfo represents a patch which is used to generate patch info file.

    >>> n1378737 = Note.objects.get(noteId = 1378737)
    >>> n1344907 = Note.objects.get(noteId = 1344907)
    >>> n1372118 = Note.objects.get(noteId = 1372118)

    >>> PL03 = PatchInfo.objects.create(patchId = 1, patchName = "PL01", isUpgPatch = False, isUpgPackage = True, patchOverviewNote = n1378737)
    >>> PL02 = PatchInfo.objects.create(patchId = 2, patchName = "PL02", isUpgPatch = False, isUpgPackage = False, patchOverviewNote = n1344907)
    >>> PL01 = PatchInfo.objects.create(patchId = 3, patchName = "PL03", isUpgPatch = False, isUpgPackage = True, patchOverviewNote = n1372118)

    # Get patch note list
    >>> len(PL03.getNoteList())
    2

    # Check upgrade package
    >>> PL01.isUpgPackage
    True

    # Get linked note list
    >>> len(PL03.getNoteList())
    2
    >>> PL03.getNoteList()[0].linkedNote.noteId in (1388288, 1382740)
    True
    >>> PL03.getNoteList()[0].overviewNote.noteId == 1378737
    True
    """
    patchId = models.IntegerField()
    patchName = models.CharField(max_length=50)
    isUpgPatch = models.BooleanField()
    isUpgPackage = models.BooleanField()
    patchOverviewNote = models.ForeignKey(to = "Note", to_field="noteId", related_name = 'patchOverviewNote')

    def getNoteList(self):
        return NoteLink.objects.filter(overviewNote = self.patchOverviewNote)